import java.io.PrintWriter;


public class Simulate extends Thread{
	
	private Simulator sim;
	
	public Simulate(Simulator current) {
    	super("Simulate");
    	this.sim = current;
    }

	public void run(){
		
		Location assign;
		PrintWriter EmulOut;
		PrintWriter SimOutVehicle;
		Station a;
		Machine b;
		Machine c;
		
		while(true){
			
			for(Machine mach : this.sim.registeredMachines){
				
				if(mach.isMoving()){
					
					assign = mach.move();
					EmulOut = mach.getEmulOut();
					SimOutVehicle = mach.getSimOut();

					EmulOut.println("geo fix "+ assign.getLongitude()+" "+assign.getLatitude()+" "+assign.getAltitude());
					SimOutVehicle.println("Changes "+ mach.getBattery()+" "+mach.getAltitude());

				}
				
				
			}
			
			/** Check Vehicles distances **/
			for(int d = 0; d < this.sim.registeredMachines.size()-1; d++){
				//System.out.println(d);
				b = this.sim.registeredMachines.get(d);
				for(int j=d+1; j < this.sim.registeredMachines.size(); j++){
					
					c = this.sim.registeredMachines.get(j);
					
					if(b.getLocation().getDistance(c.getLocation()) <= 0.2){
						
						/** Alt_Change <id> <address> <port> **/
						if(!b.isVehiclesToChangeAlt(c.getID())){
							System.out.println("Alt_Change "+ b.getID()+" "+c.getID());
							b.addVehiclesToChangeAlt(c.getID());
							c.addVehiclesToChangeAlt(b.getID());
							b.getSimOut().println("Alt_Change "+c.getID()+" "+c.getAddress()+" "+c.getPort());
							c.getSimOut().println("Alt_Change "+b.getID()+" "+b.getAddress()+" "+b.getPort());
							
						}
						

					}else if(b.isVehiclesToChangeAlt(c.getID())){
						
						/** Normalize <id>**/
						System.out.println("Normalize "+ b.getID()+" "+c.getID());
						b.rmVehiclesToChangeAlt(c.getID());
						c.rmVehiclesToChangeAlt(b.getID());
						b.getSimOut().println("Normalize "+ c.getID());
						c.getSimOut().println("Normalize "+ b.getID());
					}
					
					
					if(b.getLocation().getDistance(c.getLocation()) <= 0.3){
						
						/** Near_Entity <id> <address> <port> **/
						if(!b.isVehicleOnRadius(c.getID())){
							System.out.println("Near "+ b.getID()+" "+c.getID());
							b.addVehicleOnRadius(c.getID());
							c.addVehicleOnRadius(b.getID());
							b.getSimOut().println("Near_Entity "+c.getID()+" "+c.getAddress()+" "+c.getPort());
							c.getSimOut().println("Near_Entity "+b.getID()+" "+b.getAddress()+" "+b.getPort());
						}
						
					}else if(b.isVehicleOnRadius(c.getID())){
						
						/** Far_Entity <id> <address> <port> **/
						System.out.println("Far "+ b.getID()+" "+c.getID());
						b.rmVehicleOnRadius(c.getID());
						c.rmVehicleOnRadius(b.getID());
						b.getSimOut().println("Far_Entity "+c.getID()+" "+c.getAddress()+" "+c.getPort());
						c.getSimOut().println("Far_Entity "+b.getID()+" "+b.getAddress()+" "+b.getPort());
						
					}
					
				}
					
			}
			
			/** Check Vehicles distances to Stations **/
            for(int d = 0; d < this.sim.registeredStations.size(); d++){
                a = this.sim.registeredStations.get(d);
                
                for(int j=0; j < this.sim.registeredMachines.size(); j++){
                
                    b = this.sim.registeredMachines.get(j);
                    
                    
                    if(b.getLocation().getDistance(a.getLocation()) < 0.014){
                    	
                    	/** On_Station <address> <port> **/
                    	if(!b.isOnStation(a.getID())){
                    		
                    		System.out.println("On_Station "+ a.getAddress()+" "+a.getPort());
                    		b.onStation(a.getID());
                    		
                    		b.getSimOut().println("On_Station "+a.getAddress()+" "+a.getPort());
                    		
                    	}
                    	
                    }else{
                    	
                    	if(b.isOnStation(a.getID())){
                    		
                    		System.out.println("Not_On_Station");
                    		b.getSimOut().println("Not_On_Station");
                    		b.notOnStaton();
                    	}
	                    if (b.getLocation().getDistance(a.getLocation()) <= 0.3){
	                    	
	                    	/** Near_Station <id> <address> <port> **/ //VErificar ainda

	                    	if(!b.isStationNear(a.getID())){
	                    		System.out.println("Near_Station "+a.getID()+" "+ a.getAddress()+" "+a.getPort());
	                    		b.putStation(a.getID());
	                    		
	                    		b.getSimOut().println("Near_Station "+a.getAddress()+" "+a.getPort());
	                    	}
	                        
	                    } else{
	                    	
	                    	if(b.isStationNear(a.getID())){
	                    		System.out.println("Far_Station "+a.getID()+" "+ a.getAddress()+" "+a.getPort());  		
	                    		b.getSimOut().println("Far_Station "+a.getAddress()+" "+a.getPort());
	                    		b.rmStation(a.getID());
	                    	}
	                    }
                    }
                }
            }	
			
			try {
				// MUDAR para 1000
				Thread.sleep(200);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
	}
	
	
	
	
}
